package com.synjones.idcardusb_reader;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;

import com.synjones.cominterface.DeviceConnectManager;
import com.synjones.cpucard.CpuCardReader;
import com.synjones.multireaderlib.MultiReader;
import com.synjones.util.util;

public class ReadCpuCardActivity extends Activity {

    private TextView tvMsg;
    private Button BtnClear;
    private ScrollView svResult;

    CpuCardReader cpuCardReader;
    MultiReader multiReader=MultiReader.getReader();
    DeviceConnectManager deviceConnectManager=DeviceConnectManager.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read_cpu_card);
        tvMsg=(TextView) findViewById(R.id.tvMsg);
        svResult=(ScrollView) findViewById(R.id.scrollMsg);
        BtnClear=(Button) findViewById(R.id.btnClear);
        BtnClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                tvMsg.setText("");
            }
        });

        Button btn=(Button) findViewById(R.id.btnFindCpu);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!deviceConnectManager.isOpen()){
                    deviceConnectManager.open();
                }

                if(deviceConnectManager.isOpen()){
                    if(cpuCardReader==null) {
                        multiReader.setDataTransInterface(deviceConnectManager.getDataTransChannel());
                        cpuCardReader = new CpuCardReader(multiReader);
                    }
                    findCpuCard();
                }
            }
        });
        btn=(Button) findViewById(R.id.btnFindPSAM);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(deviceConnectManager.isOpen()){
                    if(cpuCardReader==null)
                        cpuCardReader=new CpuCardReader(multiReader);
                    findPSAMCard();
                }
            }
        });
        btn=(Button) findViewById(R.id.btnExeApdu);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(cpuCardReader==null) return;
                EditText et=(EditText)findViewById(R.id.etAPDU);
                String apdu=et.getText().toString();
                if(apdu.isEmpty()) return;
                CpuCardReader.ApduRet apduRet=cpuCardReader.exeCpuApdu(util.hexString2bytes(apdu));
                if(apduRet!=null){
                    tvMsg.append("执行结果:"+ util.bytesToHexString(apduRet.sw)+"\r\n");
                    if(apduRet.SwIs9000())
                        tvMsg.append("data="+util.bytesToHexString(apduRet.data)+"\r\n");
                }else {
                    tvMsg.append("执行超时\r\n");
                }
                scroll();
            }
        });

        btn=(Button) findViewById(R.id.btnExePsamApdu);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(cpuCardReader==null) return;
                EditText et=(EditText)findViewById(R.id.etAPDU);
                String apdu=et.getText().toString();
                if(apdu.isEmpty()) return;
                CpuCardReader.ApduRet apduRet= cpuCardReader.exePSAMApdu(util.hexString2bytes(apdu));
                if(apduRet!=null){
                    tvMsg.append("执行结果:"+util.bytesToHexString(apduRet.sw)+"\r\n");
                    if(apduRet.SwIs9000())
                        tvMsg.append("data="+util.bytesToHexString(apduRet.data)+"\r\n");
                }else {
                    tvMsg.append("执行超时\r\n");
                }
                scroll();
            }
        });

        btn=(Button)findViewById(R.id.btnUnloadPsam);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if(cpuCardReader==null) return;
                int ret=cpuCardReader.unloadPasm(CpuCardReader.PSAM_1);
                tvMsg.append("卸载PSAM卡执行结果:"+ret+"\r\n");
                scroll();
            }
        });


        deviceConnectManager.open();


    }

    void findCpuCard(){
        byte[]ret=cpuCardReader.findCpuCard();
        if(ret!=null){
            tvMsg.setText("找到CPU卡"+util.bytesToHexString(ret));
        }else
            tvMsg.setText("寻CPU卡失败");
        scroll();
    }
    void findPSAMCard(){
        byte[]ret=cpuCardReader.findPSAMCard(CpuCardReader.PSAM_1);
        if(ret!=null){
            tvMsg.setText("找到PSAM卡"+util.bytesToHexString(ret));
        }else
            tvMsg.setText("寻PSAM卡失败");
        scroll();
    }

    public void scroll(){

        svResult.post(new Runnable() {
            public void run() {
                svResult.fullScroll(ScrollView.FOCUS_DOWN);
            }
        });
    }


}
